#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
	PROJECT pyevocomm -

	APP Parla

	MODULE buttons

	SITE DESCRIPTION http://code.google.com/p/pyevocomm/ 

	@copyright: 2012 by archetipo <alessio.gerace@gmail.com>
	@license: New BSD License, see COPYING for details.
"""


import sys
import os
import code

import pyglet
from pyglet import graphics
from pyglet import text
from pyglet.text import caret, document, layout

import cocos
from cocos.director import director
from cocos.layer import Layer, ColorLayer
from status import status

__all__ = ['Buttons']

class Buttons(ColorLayer):

	def __init__(self):
		super(Buttons, self).__init__(32,32,32,192)
		self.tipo=None
		self.vw,self.wh = cocos.director.director.get_window_size()
		self.batch = pyglet.graphics.Batch()
		self.batchs = pyglet.graphics.Batch()
		self.currview=None
		self.span=10
		self.s=[]
		self.ss=[]
		self.select=0
		self.insel=0
		self.sel=None

	def refresh(self):
		self.batch = pyglet.graphics.Batch()
		if status.tipo=='menu':
			self.menu()
		else:
			self.lavoro()
		
	def menu(self):
		self.s=[]
		self.ss=[]
		currx=200
		curry=self.wh-150
		self.sel=None
		for i in status.mappav[status.col]:
			im=pyglet.resource.image(i)
			c=pyglet.sprite.Sprite(im,currx,curry,batch=self.batch)
			c.opacity=50
			self.s.append(c)
			curry-=(im.height+self.span)

		self.batchs = pyglet.graphics.Batch()
		imsel=pyglet.resource.image('sel_m.png')
		imstep=pyglet.resource.image('step_m.png')
		iminsel=pyglet.resource.image('insel_m.png')
		currx=200
		curry=self.wh+50
		if status.colsel==1:
			for x in self.s:
				x.opacity=50
			currx=self.s[status.line].x
			curry=self.s[status.line].y
			self.s[status.line].opacity=250
			if status.currline()==0:
				self.sel=pyglet.sprite.Sprite(imstep,currx,curry,batch=self.batchs)
			elif status.currline()==1:
				self.sel=pyglet.sprite.Sprite(iminsel,currx,curry,batch=self.batchs)					
			elif status.currline()==2:
				self.sel=pyglet.sprite.Sprite(imsel,currx,curry,batch=self.batchs)
			self.sel.opacity=150
			self.ss.append(self.sel)						
					
	def lavoro(self):
		self.s=[]
		self.ss=[]
		currx=200
		self.sel=None
		for i in status.mappav[status.currcol()]:
			im=pyglet.resource.image(i)
			c=pyglet.sprite.Sprite(im,currx,y=10,batch=self.batch)
			c.opacity=50
			self.s.append(c)
			currx+=(im.width+self.span)

		self.batchs = pyglet.graphics.Batch()
		imsel=pyglet.resource.image('sel.png')
		imstep=pyglet.resource.image('step.png')
		iminsel=pyglet.resource.image('insel.png')
		currx=200
		if status.colsel==0:
			for i in status.mappa[status.currcol()]:
				if status.insel==1:
					self.sel=pyglet.sprite.Sprite(iminsel,currx,y=10,batch=self.batchs)
					currx+=(im.width+self.span)
					self.sel.opacity=200
					self.ss.append(self.sel)
		elif status.colsel==1:
				if status.line>-1:
					for x in self.s:
						x.opacity=50
					currx=self.s[status.line].x
					self.s[status.line].opacity=250
					if status.currline()==0:
						self.sel=pyglet.sprite.Sprite(imstep,currx,y=10,batch=self.batchs)
					elif status.currline()==1:
						self.sel=pyglet.sprite.Sprite(iminsel,currx,y=10,batch=self.batchs)					
					elif status.currline()==2:
						self.sel=pyglet.sprite.Sprite(imsel,currx,y=10,batch=self.batchs)
					self.sel.opacity=150
					self.ss.append(self.sel)						
	def draw(self):
		self.batch.draw()
		if status.colsel==0:
			if self.sel<>None: 
				self.batchs.draw()
		elif status.colsel==1:
			if self.sel<>None: 
				self.batchs.draw()
